package leetcode_1_200;

public class LeeCode_111 {
    public static void main(String[] args) {
        System.out.println(minDepth(new TreeNode(3, new TreeNode(9), new TreeNode(20, new TreeNode(15), new TreeNode(7)))));
        System.out.println(minDepth(new TreeNode(2, null, new TreeNode(3, null, new TreeNode(4, null, new TreeNode(5))))));
    }

    private static int minDepth(TreeNode root) {
        if (root == null){
            return 0;
        }
        int[] min = new int[1];
        min[0] = Integer.MAX_VALUE;
        getMin(root, 1, min);
        return min[0];
    }
    private static void getMin(TreeNode node, int depth, int[] min){
        if (node.left == null && node.right == null){
            min[0] = Math.min(min[0], depth);
            return;
        }
        if (node.left != null)
            getMin(node.left , depth + 1, min);
        if (node.right != null)
            getMin(node.right, depth + 1, min);
    }

    private static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
